package com.fa.wisdom.ui;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.model.MainTabEntity;
import com.fa.wisdom.ui.fragment.HomeFragment;
import com.fa.wisdom.ui.fragment.ManagerFragment;
import com.fa.wisdom.ui.fragment.MineFragment;
import com.fa.wisdom.ui.fragment.MsgFragment;
import com.fa.wisdom.ui.fragment.ShopFragment;
import com.fa.wisdom.ui.fragment.ShopNFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.tuya.appsdk.sample.device.mgt.control.activity.DeviceMgtControlActivity;
import com.tuya.appsdk.sample.device.mgt.list.activity.DeviceMgtListActivity;
import com.tuya.appsdk.sample.device.mgt.list.adapter.DeviceMgtAdapter;
import com.tuya.appsdk.sample.device.mgt.list.tag.DeviceListTypePage;
import com.tuya.appsdk.sample.resource.HomeModel;
import com.tuya.smart.android.blemesh.api.ITuyaBlueMeshDevice;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.home.sdk.bean.HomeBean;
import com.tuya.smart.home.sdk.callback.ITuyaHomeResultCallback;
import com.tuya.smart.sdk.bean.DeviceBean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class MainActivity extends BaseActivity {
    private CommonTabLayout mTabLayout;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    @Override
    public void setListen() {

    }
    public CommonTabLayout getTabLayout() {
        return mTabLayout;
    }
    @Override
    public void setTitleBarColor() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (mFragments == null)
        {
            return;
        }
        // 查找在Fragment中onRequestPermissionsResult方法并调用
        for (Fragment fragment : mFragments)
        {
            if (fragment != null)
            {
                // 这里就会调用我们Fragment中的onRequestPermissionsResult方法
                fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mTabLayout = findViewById(R.id.tl);
    }
    @Override
    public void initData() {
        String[] mTitles = {getString(R.string.home), getString(R.string.shop), getString(R.string.task), getString(R.string.classRoom)
                , getString(R.string.me)};
//        getString(R.string.task)
        int[] mIconUnSelectIds = {//
                R.drawable.ketang02, R.drawable.shangcheng02, R.drawable.shejiao_select,
                R.drawable.shouye02, R.drawable.wode02};
        int[] mIconSelectIds = {//
                R.drawable.ketang01, R.drawable.shangcheng01, R.drawable.shejiao_unselect,
                R.drawable.shouye01, R.drawable.wode01};
        //tab的标题、选中图标、未选中图标
        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        //设置tab的标题、选中图标、未选中图标
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new MainTabEntity(mTitles[i], mIconSelectIds[i], mIconUnSelectIds[i]));
        }
        //初始化数据
        mFragments.add(new HomeFragment());
        mFragments.add(new ManagerFragment());
        mFragments.add(new ShopNFragment());
        mFragments.add(new MsgFragment());
        mFragments.add(new MineFragment());
        //给tab设置数据和关联的fragment
        mTabLayout.setTabData(mTabEntities, this, R.id.fl_change, mFragments);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
//                switch (position) {
//                    case 0:
//                        StatusBarUtil.setStatusBar(TestActivity.this, true);
//                        break;
//                    case 1:
//                        StatusBarUtil.setStatusBar(TestActivity.this, true);
//                        break;
//                    case 2:
//                        StatusBarUtil.setStatusBar(TestActivity.this, true);
//                        break;
//                    case 3:
//                        StatusBarUtil.setStatusBar(TestActivity.this, true);
//                        break;
//                    case 4:
//                        StatusBarUtil.setStatusBar(TestActivity.this, true);
//                        break;
//                }
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
   public void test(View view){
       getDevice();
   }
    //获取摄像头
    private void getDevice(){
        try{
            long homeId= HomeModel.getCurrentHome(this);
            TuyaHomeSdk.newHomeInstance(Constant.HOME_ID).getHomeDetail(new ITuyaHomeResultCallback() {
                @Override
                public void onSuccess(HomeBean homeBean) {
                    DeviceBean deviceBean=homeBean.getDeviceList().get(0);
                    Log.e("test-data", "onSuccess: "+homeBean.getDeviceList().get(0).devId );
                    Intent intent = new Intent(MainActivity.this, DeviceMgtControlActivity.class);
                    intent.putExtra("deviceId", homeBean.getDeviceList().get(0).devId);
                    MainActivity.this.startActivity(intent);
                }

                @Override
                public void onError(String errorCode, String errorMsg) {
                    Log.e("test-data", "獲取失敗: "+errorCode+"****"+errorMsg);

                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}